<!--
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2020-11-09 17:26:07
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-11-10 16:11:19
-->
<template>
  <div>
    <el-dialog
      title="模块权限编辑"
      :visible="dialogVisible"
      width="40%"
      @close="closeDig"
    >
      <el-checkbox v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
      <div style="margin: 15px 0;" />
      <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
        <el-checkbox v-for="item in moduleList" :key="item.unid" :label="item.unid" :disabled="item.isDisabled">{{ item.module_name }}</el-checkbox>
      </el-checkbox-group>
      <div class="demonstration">选择权限赋予时间</div>
      <el-date-picker
        v-model="value1"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDig">取 消</el-button>
        <el-button type="primary" @click="queDingEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { get_moduleList, get_mechanismAuth, set_mechanismAuth, edit_mechanismAuth } from '@/api/module'
import { formatDate } from '@/utils/getTime'
export default {
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        },
        teacherId: Number
    },
    data() {
        return {
            moduleList: [],
            checkAll: false,
            checkedCities: [],
            mustUnid: '',
            // 日期
            value1: '',
            AuthMo_id: null // 当前权限id
        }
    },
    watch: {
        // async teacherId(id) {
        //     var queryData = {
        //         institution_id: id
        //     }
        //     var data = await get_mechanismAuth(queryData)
        //     if (data.code == 200) {
        //         // 判断全选？
        //         if (data.data.module.length >= this.moduleList.length) {
        //             this.checkAll = true
        //         } else {
        //             this.checkAll = false
        //         }
        //         // 判断存在
        //         if (data.data.module.length != 0) {
        //             this.AuthMo_id = data.data.unid
        //             this.checkedCities = data.data.module.map(el => {
        //                 return el.module_id
        //             })
        //             this.value1 = [data.data.begin_time, data.data.end_time]
        //         } else {
        //             this.AuthMo_id = null
        //         }
        //     }
        //     console.log('查询条件', id, data)
        // }
    },
    created() {
        this.getModuleList()
    },
    methods: {
        // 查询机构权限
        async checkAuth() {
            this.checkedCities = []
            var queryData = {
                institution_id: this.teacherId
            }
            var data = await get_mechanismAuth(queryData)
            if (data.code == 200) {
                // 判断全选？
                if (data.data.module.length >= this.moduleList.length) {
                    this.checkAll = true
                } else {
                    this.checkAll = false
                }
                // 判断存在
                if (data.data.module.length != 0) {
                    this.AuthMo_id = data.data.unid
                    var arrList = data.data.module.map(el => {
                        return el.module_id
                    })
                    this.checkedCities = [...new Set(arrList)]
                    this.value1 = [data.data.begin_time, data.data.end_time]
                } else {
                    this.AuthMo_id = null
                }
            }
            console.log('查询条件', this.teacherId, data)
        },
        // 拿到模块
        async getModuleList() {
            var dataList = await get_moduleList({})
            console.log('模块列表：', dataList)
            if (dataList.code == 200) {
                this.moduleList = dataList.data.list
                this.moduleList.forEach((el, index) => {
                    if (el.module_name == '人事管理') {
                        this.mustUnid = el.unid
                        this.checkedCities.push(el.unid)
                        this.moduleList[index].isDisabled = true
                    } else {
                        this.moduleList[index].isDisabled = false
                    }
                })
            }
        },
        closeDig() {
            this.$emit('closeFireStaff')
        },
        handleCheckAllChange(val) {
            function cb(list) {
                var arr = []
                for (let i = 0; i < list.length; i++) {
                    let el = list[i]
                    if (el.module_name != '人事管理') {
                        arr.push(el.unid)
                    }
                }
                return arr
            }
            this.checkedCities = val ? cb(this.moduleList).concat([this.mustUnid]) : [this.mustUnid]
            console.log(this.checkedCities)
        },
        handleCheckedCitiesChange(value) {
            let checkedCount = value.length
            this.checkAll = checkedCount === this.moduleList.length
        },
        async queDingEdit() {
            if (this.value1.length == 0) {
                return this.$alert('请选择权限赋予时间', '提示', {
                    confirmButtonText: '确定',
                    callback: action => {}
                })
            }
            var fistTime = formatDate(this.value1[0])
            var lastTime = formatDate(this.value1[1])
            var queryData = {
                institution_id: this.teacherId,
                module: this.checkedCities,
                begin_time: fistTime,
                end_time: lastTime
            }
            var data
            // 判断是否更新
            console.log('判断是否更新', this.AuthMo_id)
            if (this.AuthMo_id) {
                queryData.unid = this.AuthMo_id
                data = await edit_mechanismAuth(queryData)
                console.log('修改权限：', data)
            } else {
                data = await set_mechanismAuth(queryData)
                console.log('添加权限：', data)
            }

            if (data.code == 200) {
                this.value1 = []
                this.checkedCities = [this.mustUnid]
                this.checkAll = false
                this.closeDig()
                this.$message({
                    message: '操作成功',
                    customClass: 'customClass',
                    type: 'success'
                })
            }
        }
    }
}
</script>

<style scoped lang='scss'>
.demonstration{
    margin: 35px 0 20px 0;
    font-size: 15px;
    font-weight: 600;
}
</style>
